package com.gitee.wsl.compose.ui.button

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.material.icons.Icons
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.unit.dp
import com.gitee.wsl.compose.icon.outlined.ArrowDown
import com.gitee.wsl.compose.ui.base.Button

@Composable
fun Button.Expandable(
    text: String,
    modifier: Modifier = Modifier,
    subItem: @Composable (onDismissRequest: () -> Unit) -> Unit
) {
    var isShowSubItem by remember { mutableStateOf(false) }
    val arrowRotateDegrees: Float by animateFloatAsState(if (isShowSubItem) -90f else 0f,
        label = ""
    )

    Column(modifier = modifier) {
        DropdownMenu(
            expanded = isShowSubItem,
            onDismissRequest = {
                isShowSubItem = false
            },
            modifier = Modifier.background(MaterialTheme.colorScheme.surface)
        ) {
            Column(
                modifier = Modifier.background(MaterialTheme.colorScheme.surface).padding(horizontal = 8.dp, vertical = 4.dp)
            ) {
                subItem {
                    isShowSubItem = false
                }
            }
        }

        OutlinedButton(onClick = {
            isShowSubItem = !isShowSubItem
        }) {
            Text(text = text)
            Icon(imageVector = Icons.Outlined.ArrowDown, contentDescription = text, modifier = Modifier.rotate(arrowRotateDegrees))
        }

    }
}